var app = getApp();
Page({
  data: {
    selectedIndex:0
  },
  onLoad: function (options) {
    let that = this
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight,
    });
    that.setData({
      userId: options.userId,
      username: options.username,
      telephone: options.telephone,
      address: options.address,
      remark: options.remark,
      productIdAndNumList: options.productIdAndNumList,
      type: options.type,
      payChannel: options.payChannel,
      orderId: options.orderId,
      ptype: options.ptype,
      price: options.price,
      balance: wx.getStorageSync("balance"),
    })
  },
  onShow:function(){
    var that = this;
  },
  onReady: function () {
    var that = this;
  },
  navBack: function () {
    var that = this;
    wx.navigateBack({

    })
  },
  // 单个选中
  change: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    console.info(index);
    if (that.data.selectedIndex != index) {
      that.setData({
        index: index,
        selectedIndex: index
      })
    } else {
      return false;
    }
  },
  immeBuy:function(e){
    var  that = this;
    if (that.data.selectedIndex == 1) {
      var payChannel = 5003;
    } else {
      var payChannel = 5005;
    }
    console.info(that.data.ptype);
    if (that.data.ptype == 0 || that.data.ptype == undefined || that.data.ptype == 'undefined') {
      wx.showLoading({
        title: '正在提交...',
        icon: 'loading',
      })
      console.info(that.data.shoppingCart);
      if (that.data.shoppingCart == 'undefined' || that.data.shoppingCart == undefined) {
        var shoppingCart = false;
      } else {
        var shoppingCart = that.data.shoppingCart
      }
      wx.request({
        url: app.globalData.url + "submitOrder",
        method: "GET",
        data: {
          userId: that.data.userId,
            username: that.data.username,
            telephone: that.data.telephone,
            address: that.data.address,
            remark: that.data.remark,
            productIdAndNumList: that.data.productIdAndNumList,
            type: that.data.type,
            payChannel: payChannel
        },
        success: function (res) {
          var data = res.data.data;
          console.log(res.data);
          if (res.data.result == 0) {
            wx.hideLoading();
            wx.showToast({
              title: res.data.errmsg,
              icon: 'none'
            })

            return;
          }
          that.setData({
            orderid: data.order.orderId,
          })
          if (data.order.orderStatus != 7001) {
            that.setData({
              userId: wx.getStorageSync("userId"),
              orderId: data.order.orderId,
            })
            wx.request({
              url: app.globalData.url + 'getOrderWeixinPrepayId',
              data: {
                userId: wx.getStorageSync("userId"),
                orderId: data.order.orderId,
                orderNumber: data.order.orderNumber,
              },
              method: "POST",
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                var resultData = res.data;
                var hisId = resultData.hisId;
                if (res.data.result != 1 && res.data.result != 9) {
                  wx.showToast({
                    title: "数据异常，请稍后重试",
                    icon: 'none'
                  })
                  return;
                }
                wx.requestPayment({
                  'timeStamp': resultData.timeStamp,
                  'nonceStr': resultData.nonceString,
                  'package': resultData.packageStr,
                  'signType': resultData.signType,
                  'paySign': resultData.sign,
                  //支付成功
                  success: function (res) {
                    if ("requestPayment:ok" === res.errMsg) {
                      wx.showToast({
                        title: '支付成功',
                        icon: 'success',
                        duration: 2000,
                        success: function () {
                          if (that.data.type == 2) {
                            wx.navigateTo({
                              url: '/pages/assembleDetail/assembleDetail?orderId=' + that.data.orderid + '&back=1'
                            })
                          } else {
                            console.info(that.data.lipinka);
                            if (that.data.lipinka != 3) {
                              wx.navigateTo({
                                url: '/pages/paySeccess/paySeccess?orderId=' + that.data.orderid + '&back=1'
                              })
                            } else {
                              wx.navigateTo({
                                url: '/pages/giftCardBuySuccess/giftCardBuySuccess?orderId=' + that.data.orderid + '&back=1'
                              })
                            }

                          }

                        }
                      })
                    }
                  },
                  //支付或者取消失败
                  fail: function (res) {
                    if ("requestPayment:fail cancel" === res.errMsg) {
                      wx.showToast({
                        title: '取消支付',
                        icon: 'none',
                        duration: 2000,
                        success: function () {
                          if (that.data.type == 2) {
                            wx.navigateBack({
                            });
                            wx.request({
                              url: app.globalData.url + 'delOrder',
                              method: 'GET',
                              data: {
                                orderId: that.data.orderid,
                                userId: wx.getStorageSync("userId")
                              },
                              success: function (res) {
                                wx.navigateBack({ // 返回首页
                                  delta: 2,
                                })
                              }
                            });
                          } else {
                            wx.showToast({
                              title: "取消支付",
                              icon: 'none'
                            })
                            wx.navigateBack({
                              delta: 1,
                            });
                          }

                        }
                      })
                    } else {

                      wx.showToast({
                        title: "支付失败",
                        icon: 'none'
                      })
                    }
                  },
                })
              }
            })
          } else if (res.data.result == 1 && res.data.data.order.orderStatus == 7001) {
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 2000,
              success: function () {
                console.log('haha');
                wx.navigateTo({
                  url: '/pages/paySeccess/paySeccess?orderId=' + that.data.orderid + '&back=1'
                })
                // setTimeout(function () {
                //   wx.navigateBack({
                //     delta: 2,
                //   });
                // }, 2000) //延迟时间
              }
            })
          }
        }
      });
    } else {
      if (that.data.selectedIndex == 0) {
        wx.request({
          url: app.globalData.url + 'balancePay',
          method: 'GET',
          data: {
            userId: wx.getStorageSync("userId"),
            orderId: that.data.orderId,
          },
          success: function (res) {
            if (res.data.result == 1) {
              wx.navigateTo({
                url: '/pages/paySeccess/paySeccess?orderId=' + e.target.dataset.ordid + '&back=1'
              })
            } else {
              wx.showToast({
                title: res.data.errmsg,
                icon: 'none'
              })
            }
          }
        });
      } else {
      wx.request({
          url: app.globalData.url + 'getOrderWeixinPrepayId',
          data: {
            userId: that.data.userId,
            orderId: that.data.orderId,
            orderNumber:0
          },
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            var resultData = res.data;
            var hisId = resultData.hisId;
            if (res.data.result != 1 && res.data.result != 9) {
              wx.showToast({
                title: "数据异常，请稍后重试",
                icon: 'none'
              })
              return;
            }
            wx.requestPayment({
              'timeStamp': resultData.timeStamp,
              'nonceStr': resultData.nonceString,
              'package': resultData.packageStr,
              'signType': resultData.signType,
              'paySign': resultData.sign,
              //支付成功
              success: function (res) {
                if ("requestPayment:ok" === res.errMsg) {
                  wx.showToast({
                    title: '支付成功',
                    icon: 'success',
                    duration: 2000,
                    success: function () {
                      setTimeout(function () {
                        wx.navigateBack({

                        });
                      }, 2000) //延迟时间
                    }
                  })
                }
              },
              //支付或者取消失败
              fail: function (res) {
                if ("requestPayment:fail cancel" === res.errMsg) {
                  wx.showToast({
                    title: '取消支付',
                    icon: 'none',
                    duration: 2000,
                    success: function () {
                      setTimeout(function () {
                        wx.navigateBack({

                        });
                      }, 2000) //延迟时间
                    }
                  })
                  wx.navigateBack({

                  });
                } else {
                  wx.showToast({
                    title: "支付失败",
                    icon: 'none'
                  })
                }
              },
            })
          }
        })
      }
    }
    
  },
})


